﻿using System;
using System.Web;
using System.Xml;
using Framework.Library.Base.Base;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAO;
using System.Collections.Generic;
using Framework.Library.Core.Config;
using System.Reflection;

namespace Framework.Library.Base.Helper
{
    public class ConfigHelper : BaseClass
    {
        public List<PortalSetting> lstApp = new List<PortalSetting>();
        public ConfigHelper()
        {
            lstApp = ManagerFactory.PortalSettingManager.GetSettings(Configurations.PORTALID);
            if (lstApp == null)
                lstApp = new List<PortalSetting>();
        }
        public ConfigHelper(int portalId)
        {
            lstApp = ManagerFactory.PortalSettingManager.GetSettings(portalId);
            if (lstApp == null)
                lstApp = new List<PortalSetting>();
        }

        private PortalSetting GetSetting(string key)
        {
            PortalSetting info = lstApp.Find(x => x.NAME == key);
            return info;
        }
        private PortalSetting GetSetting(string key, string defaultValue)
        {
            return (GetSetting(key) ?? new PortalSetting() { NAME = key, VALUE = defaultValue, DESCRIPTION = string.Empty });
        }
        private PortalSetting GetSetting(string key, string defaultValue, string defaultDescription)
        {
            return (GetSetting(key) ?? new PortalSetting() { NAME = key, VALUE = defaultValue, DESCRIPTION = defaultDescription });
        }
        public PortalSetting AdminMail
        {
            get
            {
                return GetSetting("MailAdmin", "mr.dungnq@ncs.com.vn");
            }
        }
        public PortalSetting UploadDir
        {
            get
            {
                return GetSetting("UploadDir", "/Uploads/");
            }
        }
        public PortalSetting Version
        {
            get
            {
                long code = Convert.ToInt64(GetSetting("Version", "0x01000212").VALUE, 16);
                string version = string.Empty;

                if ((code & 0xF0) > 0)
                {
                    version = String.Format("{0}.{1}.{2}.{3}", (code >> 24) & 0xFF, (code >> 16) & 0xFF,
                                            (code >> 8) & 0xFF, (code >> 4) & 0x0F);
                }
                else
                {
                    version = String.Format("{0}.{1}.{2}", (code >> 24) & 0xFF, (code >> 16) & 0xFF, (code >> 8) & 0xFF);
                }

                if ((code & 0x0F) > 0)
                {
                    if ((code & 0x0F) == 1)
                    {
                        // alpha release...
                        version += " alpha";
                    }
                    else if ((code & 0x0F) == 2)
                    {
                        version += " beta";
                    }
                    else
                    {
                        // Add Release Candidate
                        version += string.Format(" RC{0}", (code & 0x0F) - 2);
                    }
                }
                return new PortalSetting() { NAME = "Version", VALUE = version, DESCRIPTION = string.Empty };
            }
        }

        public PortalSetting CopyRight
        {
            get
            {
                return GetSetting("CopyRight", "© 2009 NightStalker. All rights reserved.");
            }
        }
        public PortalSetting PrefixMember
        {
            get
            {
                return GetSetting("MEMBER_PREFIX", "TEENIO_");
            }
        }
        public PortalSetting CheckIP
        {
            get
            {
                return GetSetting("CheckIP", "False");
            }
        }
        public PortalSetting LicencePath
        {
            get
            {
                return GetSetting("Licence", "/Admin/Credentials/Licence.config");
            }
        }
        public PortalSetting SiteName
        {
            get
            {
                return GetSetting("SiteName", "NightStalker");
            }
        }

        public PortalSetting MediaFilters(string type)
        {            
            return GetSetting("filter_" + type, "swf,flv,fla,bmp,gif,jpg,jpeg,png,avi,flv,mov,mp3,mp4,mpeg,mpg,ogg,wav,wma,wmv,doc,docs,txt,xls,xlsx,ppt,pptx,pdf");
        }
        public PortalSetting ThumbWidth
        {
            get
            {
                return GetSetting("ThumbWidth", "75");
            }
        }

        public PortalSetting ThumbHeight
        {
            get
            {
                return GetSetting("ThumbHeight", "75");
            }
        }

        public PortalSetting SpecialWidth
        {
            get
            {
                return GetSetting("SpecialWidth", "53");
            }
        }

        public PortalSetting SpecialHeight
        {
            get
            {
                return GetSetting("SpecialHeight", "43");
            }
        }

        public PortalSetting ListWidth
        {
            get
            {
                return GetSetting("ListWidth", "75");
            }
        }

        public PortalSetting ListHeight
        {
            get
            {
                return GetSetting("ListHeight", "0");
            }
        }

        public PortalSetting IsEvent
        {
            get
            {
                return GetSetting("IsEvent", "false");
            }
        }

        public PortalSetting ViewCost
        {
            get
            {
                return GetSetting("ViewCost", "1");
            }
        }
        public PortalSetting MonthlyCost
        {
            get
            {
                return GetSetting("MonthlyCost", "1");
            }
        }

        public string PortalPath(int portal)
        {
            return Configurations.ROOTDIR + "Default.aspx?p=" + portal + ((Configurations.MULTILANGUAGE) ? "&l=" + Configurations.LANGID : string.Empty);
        }
        public string MenuPath(int portal, int menu)
        {
            return PortalPath(portal) + "&m=" + menu;
        }
        public string MenuPath(int portal, int menu, string param)
        {
            return MenuPath(portal, menu) + param;
        }
        public string MenuPath(int portal, int menu, int type, string param)
        {
            return MenuPath(portal, menu) + ((type == 0) ? string.Empty : "&t=" + type) + param;
        }
        public string ArticlePath(int portal, int menu, int article)
        {
            return ArticlePath(portal, menu, (int)PortalLayout.Article, article);
        }
        public string ArticlePath(int portal, int menu, int type, int article)
        {
            return MenuPath(portal, menu, type, string.Empty) + "&a=" + article;
        }
        public string EventPath(int portal, int menu, int eventId)
        {
            return MenuPath(portal, menu, (int)EventLayout.Default, string.Empty) + "&e=" + eventId;
        }
    }
}